#include "palette.h"
#include "video.h"
#include <stdio.h>

#define palp(paladdress,ramaddress,colours)  ROMpals[pcount].pal=paladdress; ROMpals[pcount].lineload=ramaddress; ROMpals[pcount].length=colours; pcount++;

ROMpalette ROMpals[19]; //19 pals in the sonic ROM
int pcount=0;

byte Pal_SegaBG[] = {
0x0e,0xee,0x0e,0xee,0x0e,0xee,0x0e,0xee,0x0e,0xee,0x0e,0xee,0x0e,0xee,0x0e,
0xee,0x0e,0xee,0x0e,0xee,0x0e,0xee,0x0e,0xee,0x0e,0xee,0x0e,0xee,0x0e,0xee,
0x0e,0xee,0x0e,0xee,0x0e,0xee,0x0e,0xee,0x0e,0xee,0x0e,0xee,0x0e,0xee,0x0e,
0xee,0x0e,0xee,0x0e,0xee,0x0e,0xee,0x0e,0xee,0x0e,0xee,0x0e,0xee,0x0e,0xee,
0x0e,0xee,0x0e,0xee,0x0e,0xee,0x0e,0xee,0x0e,0xee,0x0e,0xee,0x0e,0xee,0x0e,
0xee,0x0e,0xee,0x0e,0xee,0x0e,0xee,0x0e,0xee,0x0e,0xee,0x0e,0xee,0x0e,0xee,
0x0e,0xee,0x0e,0xee,0x0e,0xee,0x0e,0xee,0x0e,0xee,0x0e,0xee,0x0e,0xee,0x0e,
0xee,0x0e,0xee,0x0e,0xee,0x0e,0xee,0x0e,0xee,0x0e,0xee,0x0e,0xee,0x0e,0xee,
0x0e,0xee,0x0e,0xee,0x0e,0xee,0x0e,0xee
};


byte Pal_Title[] = {
0x0a,0x20,0x06,0x00,0x0c,0x00,0x0e,0x44,0x0e,0x66,0x0e,0x88,0x0e,0xee,0x00,
0xae,0x00,0x6a,0x00,0x26,0x00,0xee,0x0e,0xaa,0x00,0x0c,0x00,0x06,0x00,0x02,
0x00,0x00,0x00,0x00,0x0c,0x00,0x0e,0x22,0x0e,0x44,0x0e,0x66,0x0e,0x88,0x0e,
0xee,0x0a,0xaa,0x08,0x88,0x06,0x66,0x04,0x44,0x02,0x48,0x08,0xae,0x06,0x8c,
0x00,0x00,0x00,0x0e,0x08,0x00,0x00,0x02,0x0e,0xee,0x00,0x26,0x00,0x48,0x00,
0x6c,0x00,0x8e,0x00,0xce,0x0c,0x42,0x0e,0x86,0x0e,0xca,0x0e,0xec,0x00,0x40,
0x00,0x60,0x00,0xa4,0x00,0xe8,0x0c,0x82,0x0a,0x02,0x0c,0x42,0x0e,0x86,0x0e,
0xca,0x0e,0xec,0x0e,0xee,0x0e,0xac,0x0e,0x8a,0x0e,0x68,0x00,0xe8,0x00,0xa4,
0x00,0x02,0x00,0x26,0x00,0x6c,0x00,0xce
};


byte Pal_LevelSel[] = {
0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x02,0x02,0x24,0x02,0x24,0x04,0x46,0x04,
0x46,0x02,0x24,0x02,0x24,0x04,0x46,0x06,0x68,0x02,0x24,0x00,0x02,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x02,0x24,0x02,0x24,0x04,0x46,0x06,
0x68,0x02,0x24,0x04,0x46,0x02,0x24,0x00,0x02,0x02,0x24,0x04,0x46,0x02,0x24,
0x00,0x00,0x02,0x24,0x00,0x00,0x00,0xee,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0xee,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0e,0xec,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x0e,0xec,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
};

byte Pal_Sonic[] = {
0x00,0x00,0x00,0x00,0x08,0x22,0x0a,0x44,0x0c,0x66,0x0e,0x88,0x0e,0xee,0x0a,
0xaa,0x08,0x88,0x04,0x44,0x08,0xae,0x04,0x6a,0x00,0x0e,0x00,0x08,0x00,0x04,
0x00,0xee
};

byte Pal_GHZ[] = {
0x08,0x00,0x00,0x00,0x02,0x42,0x04,0x64,0x06,0x86,0x08,0xc8,0x0e,0xee,0x0a,
0xaa,0x08,0x88,0x04,0x44,0x08,0xea,0x04,0x6a,0x00,0xee,0x00,0x88,0x00,0x44,
0x00,0x0e,0x0e,0x80,0x00,0x02,0x0e,0xee,0x00,0x26,0x00,0x48,0x00,0x6c,0x00,
0x8e,0x00,0xce,0x0a,0x86,0x0e,0x86,0x0e,0xa8,0x0e,0xca,0x00,0x40,0x00,0x60,
0x00,0xa4,0x00,0xe8,0x0c,0x82,0x0a,0x02,0x0c,0x42,0x0e,0x86,0x0e,0xca,0x0e,
0xec,0x0e,0xee,0x0e,0xac,0x0e,0x8a,0x0e,0x68,0x00,0xe8,0x00,0xa4,0x00,0x02,
0x00,0x26,0x00,0x6c,0x00,0xce
};

void PalLoadBG(int id)
{
	for (int lp=0; lp<=ROMpals[id].length; lp++)
	{
		setbcRam((lp/16)+ROMpals[id].lineload, lp%16,((ROMpals[id].pal[lp*2])<<8)+(ROMpals[id].pal[(lp*2)+1]));
	}
}

void PalLoadFG(int id)
{
	for (int lp=0; lp<=ROMpals[id].length; lp++)
	{
	     setfcRam((lp/16)+ROMpals[id].lineload, lp%16,((ROMpals[id].pal[lp*2])<<8)+(ROMpals[id].pal[(lp*2)+1]));
	     #ifdef debug_palette
         printf("foregroundpal.line[(lp/16)+ROMpals[id].lineload = %i].color[lp%16 = %i]=((ROMpals[id].pal[lp*2])<<8)+(ROMpals[id].pal[(lp*2)+1]) = %i\n",(lp/16)+ROMpals[id].lineload,lp%16,((ROMpals[id].pal[lp*2])<<8)+(ROMpals[id].pal[(lp*2)+1])); 
	     #endif
    }
}

void pcPalettes()
{
palp(Pal_SegaBG,0,63); // 0 - Sega logo
palp(Pal_Title,0,63);// 1 - title screen
palp(Pal_LevelSel,0,63);// 2 - level select
palp(Pal_Sonic,0,15);// 3 - Sonic
//Pal_Levels:
palp(Pal_GHZ,1,47);// 4 - GHZ
/*palp(Pal_LZ,1,47);// 5 - LZ
palp(Pal_MZ,1,47);// 6 - MZ
palp(Pal_SLZ,1,47);// 7 - SLZ
palp(Pal_SYZ,1,47);// 8 - SYZ
palp(Pal_SBZ1,1,47);// 9 - SBZ1
//zonewarning Pal_Levels,8
palp(Pal_Special,0,63);// $A (10) - special stage
palp(Pal_LZWater,0,63);// $B (11) - LZ underwater
palp(Pal_SBZ3,1,47);// $C (12) - SBZ3
palp(Pal_SBZ3Water,0,63);// $D (13) - SBZ3 underwater
palp(Pal_SBZ2,1,$47);// $E (14) - SBZ2
palp(Pal_LZSonWater,0,15);// $F (15) - LZ Sonic underwater
palp(Pal_SBZ3SonWat,0,15);// $10 (16) - SBZ3 Sonic underwater
palp(Pal_SSResult,0,63);// $11 (17) - special stage results
palp(Pal_Continue,0,31);// $12 (18) - special stage results continue
palp(Pal_Ending,0,63);// $13 (19) - ending sequence*/
//worry about these later
}

long palColor2RGB(word color)
{
     
     word rval=(color&0x0F00)>>8;     //0x000X, X=nybble represent color val
     word gval=(color&0x00F0)>>4;
     word bval=color&0x000F;
     rval*=0xF; gval*=0xF; bval*=0xF;
     
     long finalcolor=(rval)+(gval<<8)+(bval<<16);
     
     return finalcolor;
}

//byte Pal_GHZ[] = {
